/**
 * @Author:zhr	
 * @Date:2012-5-2
 */
package com.sys.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sys.db.dao.UserInfoDao;
import com.sys.db.daoImpl.UserInfoDaoImpl;
import com.sys.pojo.UserInfo;

/**
 * @author zhr
 * @Date 2012-5-2 at BJBank
 */
public class CheckLoginServerlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		action(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		action(req, resp);
	}
	private void action(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		String sys_flag = req.getParameter("sys_flag");
		String user_name = req.getParameter("user_name");
		String user_pass = req.getParameter("user_pass");
		String message = "";
		boolean f = false;
		try{
			UserInfoDao userDAO = new UserInfoDaoImpl();
			UserInfo user = userDAO.findByUserName(user_name);
			if(user != null){
				if(!user.getUserPwd().equals(user_pass)){
					message = "密碼錯誤！";
				}else{
					f = true;
				}		
			}else{
				message = "用戶不存在！";
			}
		}catch(Exception e){
			throw new ServletException(e.getMessage());
		}
		req.setAttribute("message", message);
		if(f){
			Integer flag = Integer.parseInt(sys_flag);
			String url = "";
			if(flag == 1){
				url = "hr.jsp";
			}else if(flag == 2){
				url = "iufo.jsp";
			}else if(flag == 3){
				url = "zizhu.jsp";
			}
			req.getRequestDispatcher(url).forward(req, resp);
		}else{
			req.getRequestDispatcher("fail.jsp").forward(req, resp);
		}
	}	
}
